import { createStore } from 'vuex';

export default createStore({
  state: {
    isLoggedIn: false,
    user: null,
  },
  mutations: {
    login(state,userInfo) {
      state.isLoggedIn = true;
      state.user =  userInfo;
    },
    logout(state) {
      state.isLoggedIn = false;
      state.user = null;
      localStorage.removeItem('authToken');
    },
     updateUser(state, updatedUser) {
      state.user = updatedUser;
    }
},
  actions: {
    logout({ commit }, router) {
      commit('logout');
      router.push('/login'); // 跳转到登录页面
    },
    saveUserInfo({ commit }, userInfo){
      commit('login', userInfo);
    },
    updateUserInfo({ commit }, updatedUser) {
      commit('updateUser', updatedUser);
    }
  }
  
})